package com.atguigu.mylock;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Resource {

    private int flag = 1;

    private Lock lock = new ReentrantLock();

    private Condition c1 = lock.newCondition();
    private Condition c2 = lock.newCondition();
    private Condition c3 = lock.newCondition();

    public void print5(int loop) throws InterruptedException {
        lock.lock();

        if (flag != 1){
            c1.await();
        }

        try {
            for (int i = 1;i<=5;i++){
                System.out.println(Thread.currentThread().getName()+",打印i:"+i+",第"+loop+"轮");
            }
            flag = 2;
            c2.signal();
        }finally {
            lock.unlock();
        }
    }
    public void print10(int loop) throws InterruptedException {
        lock.lock();

        if (flag != 2){
            c2.await();
        }

        try {
            for (int i = 1;i<=10;i++){
                System.out.println(Thread.currentThread().getName()+",打印i:"+i+",第"+loop+"轮");
            }
            flag = 3;
            c3.signal();
        }finally {
            lock.unlock();
        }
    }
    public void print15(int loop) throws InterruptedException {
        lock.lock();

        if (flag != 3){
            c3.await();
        }

        try {
            for (int i = 1;i<=15;i++){
                System.out.println(Thread.currentThread().getName()+",打印i:"+i+",第"+loop+"轮");
            }
            flag = 1;
            c1.signal();
        }finally {
            lock.unlock();
        }
    }

}
